<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>更多</title>
    <link rel="stylesheet" type="text/css" href="../../css/aui2/aui.css"/>
    <style>
        body{
            
        }
    </style>
</head>
<body>
    <section class="aui-content" id="courses_list">
    </section>
    <script id="push_courses_template" type="text/x-dot-template">
        {{ for(var i=0;i<it.data.length;i++) { }}
        <div class="aui-card-list" tapmode onclick="openCourse({{=count+i}})">
            <div class="aui-card-list-content">
                <img src={{=it.path+it.data[i].cover}} />
            </div>
            <div class="aui-card-list-header">
                {{=it.data[i].title}}
            </div>
            <div class="aui-card-list-footer">
                <span>{{="推送老师:"+it.data[i].teacher}}</span><span>{{=it.data[i].start_time}}</span>
            </div>
        </div>
        {{ } }}
    </script>
</body>
<script src="../../script/doT.min.js"></script>
<script data-main="../../script/main" src="../../script/require.js"></script>
<script type="text/javascript">
    var token;
    var count=0;
    var isbottom = false;//是否到底
    var isRefresh = true;//是否刷新
    var temp_data = [];
    apiready = function(){
        require(["zepto","api","common"],function(){
            
            $zb.prefs.getPrefs('token',function(ret){
                token = ret.value;
                sendAjax();
            })
            api.setRefreshHeaderInfo({
                loadingImg: 'widget://image/refresh.png',
                bgColor: '#ccc',
                textColor: '#fff',
                textDown: '下拉刷新...',
                textUp: '松开刷新...'
            }, function(ret, err) {
                api.refreshHeaderLoadDone();
                if(ret){
                    isbottom = false;
                    isRefresh=true;
                    sendAjax();
                }
            });
            
            api.addEventListener({
                name:'scrolltobottom',
                extra:{
                    threshold:0            //设置距离底部多少距离时触发，默认值为0，数字类型
                }
            }, function(){
                isRefresh = false;
                isbottom?api.toast({
                    msg:'没有更多了'
                }):sendAjax(count);        
            });
        });
    };
    
    function sendAjax(num){
        var data = {
            values:{
                token:token,
                id:api.pageParam.id,
                appindex:num,
                appnum:10
            }
        }
        var apiname;
        if(api.pageParam.index==1){
            apiname = 'getHottestCourses';
        }else{
            apiname = 'getLatestCourses';
        }
        $zb.requestForData(data,'main',apiname,function(ret){
            if(ret&&ret.status==="OK"){
                if(ret.data.length>0){
                    var temp = {
                        path:$zb.getRes(),
                        data:ret.data
                    }
                    if(isRefresh){
                        temp_data = ret.data;
                        $("#courses_list").html(doT.template($("#push_courses_template").text())(temp));
                    }else{
                        temp_data = temp_data.concat(ret.data);
                        $("#courses_list").append(doT.template($("#push_courses_template").text())(temp));
                    }
                    if(ret.data.length===10){
                        count+=10;
                    }else{
                        isbottom = true;
                    }
                    api.parseTapmode();
                }else{
                    $("#courses_list").html("");
                }
            }
        });
    }
    function openCourse(index){
        var data = {
           values:{
               token:token,
               id:temp_data[index].id
           }
       }
       $zb.requestForData(data,'resources','getResourceDetail',function(ret){
            if(ret&&ret.status==="OK"){
                api.openWin({
                    name: 'rs_detail_win',
                    url: '../resource/rs_detail_win.html',
                    pageParam:{
                        data:ret.data,
                        index:parseInt(temp_data[index].type),
                        info:data.values,
                        img:$zb.getRes()+temp_data[index].cover
                    },
                    slidBackEnabled:false
                });
            }
       });
    }
</script>
</html>